#include <memory>
#include "Connection.hpp"
#include "Channel.hpp"
using namespace std;
class Listen : public Connection
{
public:
    Listen(uint16_t port)
        : _listen_ptr(make_unique<TcpSocket>())
    {
        _listen_ptr->BuildTcpServerMoudule(port);
        SetEvents(EPOLLIN | EPOLLET);
        SetNonBlock(_listen_ptr->Fd());
    }
    virtual void Recver()
    {
        //cout<<"Listen Recver"<<endl;
        while (true)
        {
            InetAddr client;
            auto ret = _listen_ptr->Accept(&client);
            if (ret == nullptr)
                break;
            shared_ptr<Connection> cn = make_shared<Channel>(ret->Fd(),client);
            SetNonBlock(ret->Fd());
            cn->SetEvents(EPOLLIN | EPOLLET);
            GetOwner()->AddConnection(cn);
            cn->RegisterHandler(GetHandler());
        }
        //cout<<"Listen Recver out"<<endl;
    }
    virtual void Sender()
    {}
    virtual int GetFd()
    {
        return _listen_ptr->Fd();
    }
    ~Listen()
    {
    }

private:
    unique_ptr<Socket> _listen_ptr;
};